library(rio)

ef <- import("estimation_gamma_vars.dta")

posts <- readLines("polex_cats.txt")
posts <- posts[posts != ""]
postsLR <- strsplit(posts, ":")

postsDF <- data.frame(category = character(), variable = character())

for (i in 1:length(posts)) {
  
  d <- data.frame(
    category = postsLR[[i]][1],
    variable = unlist(strsplit(postsLR[[i]][2], " "))
  )
  
  postsDF <- rbind(postsDF, d)
}

postsDF <- subset(postsDF, variable != "" | variable == "prevtimesinoffice")

postsDF$variable <- as.character(postsDF$variable)

ef$polTopPost <- apply(ef[, postsDF$variable[postsDF$category == "top posts"]], 1, max, na.rm = TRUE)
ef$polTopMinistry <- apply(ef[, postsDF$variable[postsDF$category == "top ministers"]], 1, max, na.rm = TRUE)
ef$polTopPostOrMinistry <- apply(ef[, postsDF$variable[postsDF$category %in% c("top posts", "top ministers")]], 1, max, na.rm = TRUE)

ef$countPolTopPost <- apply(ef[, postsDF$variable[postsDF$category == "top posts"]], 1, sum, na.rm = TRUE)
ef$countPolTopMinistry <- apply(ef[, postsDF$variable[postsDF$category == "top ministers"]], 1, sum, na.rm = TRUE)
ef$countPolTopPostOrMinistry <- apply(ef[, postsDF$variable[postsDF$category %in% c("top posts", "top ministers")]], 1, sum, na.rm = TRUE)

polPosts <- postsDF$variable[postsDF$category %in% c("top posts", "top ministers", "party", "minister", "regional", "parliament") | postsDF$variable %in% c("mindefence", "headsecurity", "eu_commission", "MEP", "deputyminister", "centralbankgov", "diplomat")]

postsDF["political"] <- ifelse(postsDF$variable %in% polPosts, "political", "")

ef$numberPolPosts <- apply(ef[, polPosts], 1, sum, na.rm = TRUE)
ef$polAnyExp1 <- as.integer(ef$numberPolPosts > 0)
ef$polAnyExp2 <- as.integer(ef$numberPolPosts > 1)
ef$polAnyExp3 <- as.integer(ef$numberPolPosts > 2)

ef$postsCountScale <- ef$numberPolPosts + ef$countPolTopMinistry + 2 * ef$countPolTopPost

cor(ef[, c("experienceyears", "formalexperyears", "prevtimesinoffice", "polTopPost", "polTopMinistry", "polTopPostOrMinistry", "postsCountScale", "polAnyExp1", "polAnyExp2", "polAnyExp3")], use = "pairwise.complete.obs")

export(ef, file = "estimation_file_with_polexp.dta")
